<?php

namespace api\zhengmao\controller;
use api\zhengmao\model\AddressModel;
use api\zhengmao\validate\UserValidate;
use api\zhengmao\model\UserModel;
use cmf\controller\RestBaseController;
use think\facade\Db;
use \think\facade\Filesystem;
use think\Request;

class UserController extends RestBaseController
{
    //手机号查询数据
    public function index()
    {
        $mobile = input('mobile');
        if(empty($mobile)){
            return error('请输入手机号');
        }
        $data = UserModel::where('mobile' ,$mobile)->find();
        if(empty($data)){
            return error('手机号错误');
        }
        return success('查询成功',$data);
    }

    //本地图片上传
    public function fileLocal()
    {
        $name = input('user_name');
        $mobile = input('mobile');
        $file = request()->file('image');
        $result = Filesystem::disk('public')->putFile('image',$file);
        $data = UserModel::where('mobile',$mobile)->find();
        if(empty($data)){
            return error('手机号为空');
        }
        $data->user_name =$name;
        $data->image   = $this->request->domain().'/storage'.$result;
        if($data->save()){
            $this->success('添加成功',[
                'url' =>$this->request->domain().'/storage'.$result
            ]);
        }
        return error('添加失败');
    }

    //修改用户
    public function update()
    {
        $data = input('post.');
        //判断器
        $validate = new UserValidate();
        $v = $validate->scene('update')->check($data);
        if ($v !== true) {
            return error($validate->getError());
        }
        //去除image数据
        unset($data['image']);
        //修改图片
        $image = request()->file('image');
        $result = Filesystem::disk('public')->putFile('image',$image);
        $image2 =   request()->domain().DIRECTORY_SEPARATOR.'storage'.DIRECTORY_SEPARATOR.$result;

        //修改数据库
        $edit = UserModel::where('id','=',$data['id'])->find();
        if (!$edit){
            $this->error('未查询到数据');
        }
        $edit->image = $image2;
        $edit->user_name = $data['user_name'];
        if ($edit->save()){
            return success('修改成功');
        }else{
            return error('修改失败');
        }
    }
    //收货地址
    public function address()
    {
        $data = input('post.');
        $validate = new UserValidate();
        $result = $validate->scene('address')->check($data);
        if ($result !== true) {
            return error($validate->getError());
        }
        $address = new AddressModel();

        if($address->save($data)){
            return success('添加成功');
        }else{
            return error('添加失败');
        }
    }
    public function putAddress()
    {
        $user_id = input('user_id');
        if(empty($user_id )){
            return error('参数错误');
        }
        $data = AddressModel::where('user_id',$user_id)
            ->order('id desc')
            ->select();
        if($data) {
            return success('查询成功',$data);
        }else{
            return success('查询失败');
        }
    }

    public function addMoney()
    {
        $data = input('post.');
        Db::startTrans();
        $user_id = input('user_id');
        // 获取充值金额
        $money = $data['money'];
        $reason = $data['reason'] ?? '充值';
        try {
            // 调用 common 函数处理充值逻辑
            $newBalance = add_money($user_id, $money, $reason, 1);
            Db::commit();
            return json(['status' => 'success', 'message' => '充值成功', 'new_balance' => $newBalance]);
        } catch (\Exception $e) {
            Db::rollback();
            return json(['status' => 'error', 'message' => $e->getMessage()], 500);
        }
    }

    public function record()
    {
        $user_id = input('user_id');
        // 查询用户的流水记录
        $logs = Db::name('wallet')
            ->where('user_id', $user_id)
            ->order('create_time', 'desc')
            ->select();
        if (!$logs) {
            return error('未查询到');
        }
        return success('查询成功',$logs);
    }
}
